<script>
import {del, fetchList} from '@/api/appointment'
import {mapGetters} from 'vuex'
import {getToken} from '@/utils/auth'

export default {
  data() {
    return {
      searchContent: '',
      page: 1,
      pageSize: 20,
      pageTotal: 0,
      userList: [],
      dialogImageUrl: '',
      dialogVisible: false,
      userInfo: {
        username: '',
        password: '',
        code: '',
        city: '',
        bornDate: '',
        country: '',
        isEnabled: '',
        educationFk: '1',
        mail: '',
        major: '',
        majorSkill: '',
        name: '',
        nowRoles: 0,
        sexFk: '0',
        privilege: '',
        province: '',
        resumeFk: '0',
        rolesFk: '1',
        school: '',
        sessionKey: '',
        tel: '',
        userPk: '',
        userStatusFk: '1',
        workStatusFk: '1',
        headimgurl: '',
        wx: ''
      },
      formLabelWidth: '80px',
      nowRolesOptions: [
        {
          label: '招聘者',
          value: 1
        },
        {
          label: '求职者',
          value: 0
        }
      ],
      sexOptions: [],
      educationOptions: [],
      isShow: false,
      headerImgFiles: [],
      workStatusOptions: [],
      rolesOptions: [],
      userStatusOptions: []
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    getToken,
    getData() {
      fetchList(this.page, this.pageSize)
        .then(res => {
          console.log(res)
          const msg = res.message
          msg.list.forEach(el => {
            el.patientUserId = JSON.parse(el.patientUserId)
          })
          this.userList = msg.list
          this.pageTotal = msg.total
        })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page = val
    },
    reset() {
      this.searchContent = ''
      this.page = 1
      this.pageSize = 20
      this.pageTotal = 0
      this.getData()
    },
    resetFormData() {
      this.userInfo = {
        username: '',
        password: '',
        code: '',
        city: '',
        bornDate: '',
        country: '',
        isEnabled: '',
        educationFk: '1',
        mail: '',
        major: '',
        majorSkill: '',
        name: '',
        nowRoles: 0,
        sexFk: '0',
        privilege: '',
        province: '',
        resumeFk: '0',
        rolesFk: '1',
        school: '',
        sessionKey: '',
        tel: '',
        userPk: '',
        userStatusFk: '1',
        workStatusFk: '1',
        headimgurl: '',
        wx: ''
      }
      this.headerImgFiles = []
    },
    created() {
      this.resetFormData()
      this.isShow = true
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    del(row) {
      console.log(row)
      this.$confirm('确认删除该预约')
        .then(_ => {
          del(row.reservationId)
            .then(res => {
              console.log(res)
              if (res.success) {
                this.$message({
                  type: "success",
                  message: "删除成功"
                })
                this.getData()
              } else {
                this.$message({
                  type: "error",
                  message: res.message
                })
              }
            })
        })
        .catch(_ => {
        });
    }
  },
  computed: {
    ...mapGetters([
      'roles'
    ])
  }
}
</script>

<template>
  <div class="app-container">
    <el-dialog :visible.sync="dialogVisible">
      <img :src="dialogImageUrl" alt="" width="100%">
    </el-dialog>
    <el-table :data="userList" height="75vh" style="width: 100%;margin-top: 20px">
      <el-table-column type="index" width="80"/>
      <el-table-column label="姓名" prop="patientUserId.name"/>
      <el-table-column label="预约时间" prop="reservationTime" width="200"/>
      <el-table-column label="性别" width="120">
        <template slot-scope="scope">
          <div>
            {{ scope.row.patientUserId.sexs }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="麻醉方式" prop="aneshesiaType" width="180"/>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <div class="d-flex flex-wrap">
            <el-button type="danger" @click="del(scope.row)">{{ '删除记录' }}
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="d-flex jc-end">
      <el-pagination
        :current-page="page"
        :page-size="pageSize"
        :page-sizes="[20, 50, 100, 200]"
        :total="pageTotal"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<style lang="scss" scoped>

</style>
